/*
 *
 * Worker class for displaying notifications
 *
 */
var notify = function() {
	var not_initialized = false
	var not_externals = 0;
	var not_timeout = 30000;
	var not_isWin = framework.runtime.osName.toLowerCase().match("windows");
	var not_isLinux = framework.runtime.osName.toLowerCase().match("linux");
	var not_path;
	var not_queue = [];
	function not_append_text(file, text) {
		var f, fso = framework.system.filesystem;
		if(!fso.FileExists(file))
			f = fso.CreateTextFile(file, true);
		else 
			f = fso.OpenTextFile(file, 8, true);
		f.WriteLine(text)
		f.Close();
	}
	function not_launch(obj) {
		if(vverbose) log(__FUNCTION())
		var _use_is_ok = options.getValue("TVTID.Notifiy.use_notitications").match("yes");
		var _use_dbus = options.exists("TVTID.Notifier.uses_dbus");
		if(!_use_is_ok) return
		if(_use_dbus) {
			var item = new ContentItem();
			item.heading = obj.notify_title;
			item.snippet = obj.notify_text;
			plugin.AddContentItem(item, gddItemDisplayAsNotification);
		} else if(not_isWin) {
			var WshShell = new ActiveXObject("WScript.Shell");
			var oExec = WshShell.Exec(not_path + "ggl-notifier.exe " +
				'"'+ obj.notify_title +
				'" "' + obj.notify_text +
				'" "' + strings.READ_MORE +
				'" "' + obj.notify_href +
				'" "' + (not_externals++) + 
				'" "' + not_path + 'ggl-notifier.cb"');

			while (oExec.Status == 0)
			{
				WScript.Sleep(100);
			}
			alert("Final: " + oExec.Status);
			oExec = null;
			WshShell = null;
			not_externals--;
		} else  {
			not_append_text(not_path + "ggl-notifier.txt", 
				obj.notify_title +
				'|' + obj.notify_text +
				'|' + (not_externals++) +
				'|' + storage.extract("icon_small.png") );
			//	new ActiveXObject("WScript.Shell").
			//	run("file://" + not_path + "ggl-notifier-dbus.sh");
			setTimeout(function() {
				not_externals--;
			}, not_timeout+5000)
		/*
			* PYTHON impl
			*bus.get_object("org.freedesktop.DBus","/org/freedesktop/DBus")
			notify_service = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
			self.interface = dbus.Interface(notify_service, 'org.freedesktop.Notifications')
			*/
		}
		
	}
	function not_unpack() {
		if(vverbose) log(__FUNCTION())
		not_path = options.getValue("TVTID.cache.foldername")

		var fso = framework.system.filesystem;
		var fld = fso.GetFolder(not_path)

		if(! /(\/|\\)$/.test(not_path)) not_path += (not_isWin?"\\":"/");
		if(! fso.FolderExists(not_path))
			throw not_path+ " does not exist.. please fix errors"
		if(fso.FileExists(not_path + "ggl-notifier.txt"))
			fso.DeleteFile(not_path + "ggl-notifier.txt");
		var files = [ 
		"ggl-notifier.sh",
		"ggl-notifier-dbus.sh",
		"ggl-notifier.pl",
		"ggl-notifier.bin",
		"ggl-notifier.exe"
		];
		var modded = false;
		files.forEach(function(file) {
			if(!fso.FileExists(not_path+ file)) {
				modded = true;
				fso.GetFile(storage.extract(file)).Copy(not_path + file);
			}
		});
		if(modded) {
			inst_step5();
		}

	}
	function not_run() {
		if(vverbose) log(__FUNCTION())
		if(not_initialized) return;
		not_initialized = true;
		var _use_is_ok = options.getValue("TVTID.Notifiy.use_notitications").match("yes");
		var _use_dbus = options.exists("TVTID.Notifier.uses_dbus");
		if(!_use_is_ok || _use_dbus) return;

		not_unpack();
		if(!not_isWin)
			new ActiveXObject("WScript.Shell").run("file://" + not_path + "ggl-notifier-dbus.sh");
		return;
		/*
		var rr = function() {
			log('rr');
			if(not_queue.length > 0) {
				var o = not_queue.shift();
				not_launch(o);
			}
		}
		setInterval(rr, 2000);
		*/
	}
	return {
		launch: function(title, text, href) {
			//log("LAUNCHING ("+title+","+text+","+href);
			not_queue.push({
				notify_title:title,
				notify_text: text,
				notify_href: href
			});
		},
		start:function() {
			not_run();
		}
	}
}();
